package com.shuang.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shuang.common.PageObject;
import com.shuang.mapper.DbGoodsDescribeMapper;
import com.shuang.mapper.DbGoodsMapper;
import com.shuang.mapper.DbGoodsParamMapper;
import com.shuang.mapper.DbGoodsPropertyParamMapper;
import com.shuang.pojo.DbGoods;
import com.shuang.pojo.DbGoodsDescribe;
import com.shuang.pojo.DbGoodsExample;
import com.shuang.pojo.DbGoodsExample.Criteria;
import com.shuang.pojo.DbGoodsParam;
import com.shuang.pojo.DbGoodsPropertyParam;
import com.shuang.pojo.ExtendDbGoods;
import com.shuang.pojo._ExtendDbGoods;
import com.shuang.service.DbGoodsService;
@Service("dbGoodsService")
public class DbGoodsServiceImpl implements DbGoodsService {

	
	@Resource(name="dbGoodsMapper")
	private DbGoodsMapper dbGoodsMapper;
	@Resource(name="dbGoodsDescribeMapper")
	private DbGoodsDescribeMapper dbGoodsDescribeMapper;
	@Resource(name="dbGoodsParamMapper")
	private DbGoodsParamMapper dbGoodsParamMapper;
	@Resource(name="dbGoodsPropertyParamMapper")
	private DbGoodsPropertyParamMapper dbGoodsPropertyParamMapper;
	@Override
	public Map<String, Object> insertGoods(ExtendDbGoods record) throws Exception {
		 Map<String, Object> map=new HashMap<String, Object>();
		Date date = new Date();
		record.setgAddTime(date);
		record.setgStatus((short)1);//1为待审核 2为待上架，2上架  3下架
		int result = dbGoodsMapper.insert(record);//添加商品
		
		if(result>0){
			DbGoodsDescribe record1 = new DbGoodsDescribe();//插入商品描述
			record1.setgId(record.getgId());
			record1.setDeCreateTime(date);
			record1.setDeContent(record.getDescribeData());
			int result1=dbGoodsDescribeMapper.insert(record1);
			
			if(result1>0){
				DbGoodsParam record2 = new DbGoodsParam();
				record2.setgId(record.getgId());
				record2.setGpCreateTime(date);
				record2.setGpParamData(record.getFormatData());
				int result2 = dbGoodsParamMapper.insert(record2);//添加商品规格参数
				
				if(result2>0){
					DbGoodsPropertyParam record3 = new DbGoodsPropertyParam();
					record3.setgId(record.getgId());
					record3.setGppCreateTime(date);
					record3.setGppParamData(record.getPropertyData());
					int result3=dbGoodsPropertyParamMapper.insert(record3);//添加商品属性
					
					if(result3>0){
						map.put("status", 200);
					}else{
						map.put("status", 500);
						map.put("info", "商品属性录入失败");
						throw new Exception();
					}
				}else{
					map.put("info", "商品规格录入失败");
					map.put("status", 500);
					throw new Exception();
				}
			}else{
				map.put("status", 500);
				map.put("info", "商品描述录入失败");
				throw new Exception();
			}
		}else{
			map.put("status", 500);
			map.put("info", "商品录入失败");
			throw new Exception();
		}
		
		return map;
	}
	@Override
	public PageObject<_ExtendDbGoods> selectGoodsList(int page, int rows) {
		DbGoodsExample example = new DbGoodsExample();
		example.setOrderByClause("g_status desc");
		example.setOrderByClause("g_update_time");
		// 分页处理
		PageHelper.startPage(page, rows);
		List<_ExtendDbGoods> list=dbGoodsMapper.selectExtendByExample(example);
		PageInfo<_ExtendDbGoods> pageInfo = new PageInfo<_ExtendDbGoods>(list);
		PageObject<_ExtendDbGoods> pageObject = new PageObject<_ExtendDbGoods>(
				pageInfo.getTotal(), list);
		return pageObject;
	}
	@Override
	public Map<String, Object> updateGoodsStatusById(Long gId, short status) {
		DbGoods record = new DbGoods();
		record.setgId(gId);
		record.setgStatus(status);
	   int result=dbGoodsMapper.updateByPrimaryKeySelective(record);
	   Map<String, Object> map=new HashMap<String, Object>();
	   if(result>0){
		   map.put("status", 200);
		   map.put("data", "更新成功");
	   }else{
		   map.put("status", 500);
		   map.put("data", "内部错误");
	   }
		return map;
	}
	@Override
	public Map<String, Object> updateGoods(DbGoods dbGoods) {
		dbGoods.setgUpdateTime(new Date());
		int result = dbGoodsMapper.updateByPrimaryKeySelective(dbGoods);
		 Map<String, Object> map=new HashMap<String, Object>();
		if(result>0){
			   map.put("status", 200);
			   map.put("data", "更新成功");
		   }else{
			   map.put("status", 500);
			   map.put("data", "内部错误");
		   }
		return map;
	}
	@Override
	public ExtendDbGoods selectGoodsById(Long gId) {
		ExtendDbGoods result = dbGoodsMapper.selectExtendByPrimaryKey(gId);
		return result;
	}
	@Override
	public PageObject<DbGoods> selectNewGoodsListByPage(int page, int rows) {
		DbGoodsExample example = new DbGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andGStatusEqualTo((short)3);//状态是上架
		example.setOrderByClause("g_add_time desc");
		// 分页处理
		PageHelper.startPage(page, rows);
		List<DbGoods> list=dbGoodsMapper.selectByExample(example);
		PageInfo<DbGoods> pageInfo = new PageInfo<DbGoods>(list);
		PageObject<DbGoods> pageObject = new PageObject<DbGoods>(
				pageInfo.getTotal(), list);
		return pageObject;
	}
	@Override
	public PageObject<DbGoods> selectGoodsListStatusIOK(int page, int rows) {
		DbGoodsExample example = new DbGoodsExample();
		Criteria criteria = example.createCriteria();
		criteria.andGStatusEqualTo((short)3);//状态是上架
		// 分页处理
		PageHelper.startPage(page, rows);
		List<DbGoods> result = dbGoodsMapper.selectByExample(example);
		PageInfo<DbGoods> pageInfo = new PageInfo<DbGoods>(result);
		PageObject<DbGoods> pageObject = new PageObject<DbGoods>(
				pageInfo.getTotal(), result);
		return pageObject;
	}

}
